﻿using System;
using System.Collections.Generic;
using System.Linq;
using FlanProjectData.Managers;
using FlanProjectData.Helpers;
using FlanProjectData.GameObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace FlanProject.ScreenMenu
{
    class PauseScreen : InterfaceScreen
    {
        private static Logger logger = new Logger("PauseScreen");

		// List screen who can be accessible with this screen
		protected new enum AccessibleForm
		{
			SAVEGAME, 
			LOADGAME, 
			OPTION, 
			CREDIT,
			EXITGAME, 
		}

        #region attributs
        private float distanceBetween2Button = (float)0.12;	//Define the distance in % of 2 button
		
		private float widthDistanceButton = (float)0.36;	//Define the distance in % from the right-side & the button
		private float heightDistanceButton = (float)0.24;	//Define the distance in % from the top-side & the FIRST button
		
		private float heightPercentButton = (float)0.09;	//Define the height in % of the button
		private float widthPercentButton = (float)0.43;		//Define the width in % of the button
		
		private float widthDistanceText = (float)0.04;
		private float heightDistanceText = (float)0.14;

        private float policeSize = 1;

        private Party team = null;

        #endregion

        #region Ctor
        public PauseScreen()
		{
			int i = 0;
            //team = maTeam;
			#region add buttons

			#region NewGameButton

			this.lstMenuEntrySelectable.Add(new MenuEntry("Save Game",
				new Vector2(((float)windowsWidth * widthDistanceButton),
					((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
				new Vector2((float)windowsWidth * widthPercentButton,
                    (float)windowsHeight * heightPercentButton), policeSize));
            this.lstMenuEntrySelectable[i - 1].Selected += this.saveGame;
			#endregion
			
			#region LoadGameButton

			this.lstMenuEntrySelectable.Add(new MenuEntry("Load Game",
				new Vector2(((float)windowsWidth * widthDistanceButton),
					((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
				new Vector2((float)windowsWidth * widthPercentButton,
                    (float)windowsHeight * heightPercentButton), policeSize));
            this.lstMenuEntrySelectable[i - 1].Selected += this.LoadingScreen;

			#endregion
			
			#region OptionButton

			this.lstMenuEntrySelectable.Add(new MenuEntry("Options",
				new Vector2(((float)windowsWidth * widthDistanceButton),
					((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
				new Vector2((float)windowsWidth * widthPercentButton,
                    (float)windowsHeight * heightPercentButton), policeSize));
			this.lstMenuEntrySelectable[i - 1].Selected += this.OptionScreen;

			#endregion
			
			#region CreditButton

			this.lstMenuEntrySelectable.Add(new MenuEntry("Credits",
				new Vector2(((float)windowsWidth * widthDistanceButton),
					((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
				new Vector2((float)windowsWidth * widthPercentButton,
                    (float)windowsHeight * heightPercentButton), policeSize));
			this.lstMenuEntrySelectable[i - 1].Selected += this.CreditScreen;

			#endregion

			#region QuitGameButton
			this.lstMenuEntrySelectable.Add(new MenuEntry("Quit Current Game",
				new Vector2(((float)windowsWidth * widthDistanceButton),
					((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
				new Vector2((float)windowsWidth * widthPercentButton,
                    (float)windowsHeight * heightPercentButton), policeSize));
			
			this.lstMenuEntrySelectable[i - 1].Selected += this.ExitGame;
			#endregion


			#endregion

			#region add text

			this.lstMenuEntryUnselectable.Add(new MenuEntry("Pause",
				new Vector2((float)windowsWidth * widthDistanceText,
					(float)windowsHeight * heightDistanceText),
                true, Color.Black, policeSize));

			#endregion

			this.Visible = true;
		}
		#endregion

		#region Overrided HandleInput

		public override void HandleInput()
		{
            logger.Info("touche");
			if (InputManager.IsActionTriggered(InputManager.Action.Up))
			{
				selectedIndex = (selectedIndex - 1 + lstMenuEntrySelectable.Count) % lstMenuEntrySelectable.Count;
				logger.Debug(selectedIndex.ToString());
			}
			else if (InputManager.IsActionTriggered(InputManager.Action.Down))
			{
				selectedIndex = Math.Abs(selectedIndex + 1) % lstMenuEntrySelectable.Count;
				logger.Debug(selectedIndex.ToString());
			}
			// Validation of selected index
			else if (InputManager.IsActionTriggered(InputManager.Action.Ok))
			{
				logger.Info("Validation on :" + selectedIndex.ToString());
                logger.Debug("texte: " + this.lstMenuEntrySelectable[selectedIndex].message);
				
				// Execute MenuEntryAction
				this.lstMenuEntrySelectable[selectedIndex].OnSelectEntry();
			}
			else if (InputManager.IsActionTriggered(InputManager.Action.Exit))
			{
				logger.Warn("Fermeture du programme");
				this.ExitScreen();
			}
		}

		#endregion

		#region Overrided LoadContent

		public override void LoadContent(ContentManager cm)
		{
			this.background = cm.Load<Texture2D>(@"Sprites\Menus\commonBackgroundMenu");

			// calculate the scale of the background
			base.calcScaleBackground(windowsHeight, windowsWidth);

			// Use virtual definition
			base.LoadContent(cm);
		}

		#endregion

		#region Overrided Draw

		#endregion

		/*public override InterfaceManager ScreenIsChanging()
		{
			InterfaceManager im = null;
			if (this.OnSwitch)
			{
				//im = new
			}
			return im;
		}*/

		#region Method To Switch Windows

		private void LoadingScreen(AccessibleForm a)
		{
			switch (a)
			{
				case AccessibleForm.SAVEGAME :
                    logger.Info("Sauvegarde du fichier");
					break;
				case AccessibleForm.LOADGAME :
                    this.imChild = new LoadingScreen();
					break;
				case AccessibleForm.OPTION :
					logger.Info("Loading Option screen");
					this.imChild = new OptionMenuScreen();
					break;
				case AccessibleForm.CREDIT :
					logger.Info("Loading Credit screen");
					this.imChild = new CreditMenuScreen();
					break;
				case AccessibleForm.EXITGAME :
                    //team = null;// on desinstancie l'equipe actuel
					break;
				default :
					logger.Warn("AccessibleForm : " + a.ToString() +" not exist");
					break;
			}
			this.imChild.LoadContent(this.Content);
			this.Visible = false;
			this.imChild.Visible = true;

		}


		public void CreditScreen(object sender, EventArgs e)
		{
			this.LoadingScreen(AccessibleForm.CREDIT);
		}

		public void OptionScreen(object sender, EventArgs e)
		{
			this.LoadingScreen(AccessibleForm.OPTION);
		}

        public void LoadingScreen(object sender, EventArgs e)
        {
            this.LoadingScreen(AccessibleForm.LOADGAME);
        }



       
		#endregion
	}
}
